
import os

def get_memory(pid): # 输出窗口命令 返回获取值
    cmd='adb shell dumpsys meminfo ' + pid
    contents = os.popen(cmd)
    for line in contents.readlines():
        if 'TOTAL   ' in line:
            return line.split(' ')[11].strip()

def get_cpu(package): # 输入命令获取使用cpu的值
    cmd='adb shell top -n 1'
    content = os.popen(cmd)
    for line in content.readlines():
        if package in line:
            infos = line.split(" ")
            try:
                return infos[infos.index("S") + 1].strip(), infos[infos.index("S") + 3].strip()
            except:
                return infos[infos.index("R") + 1].strip(), infos[infos.index("R") + 4].strip()

def get_electric_quantity(): # 输入命令获取使用电量的值
    cmd = 'adb shell dumpsys battery'
    contents = os.popen(cmd)
    for line in contents.readlines():
        if 'level: ' in line:
            return line.split(' ')[3].strip()

def get_meminfo_total(): # 输入命令获取设备总内存的值
    cmd = 'adb shell cat /proc/meminfo'
    contents = os.popen(cmd)
    for line in contents.readlines():
        if 'MemTotal:' in line:
            return line.split(' ')[8].strip()

def get_pid(package):
    contents = os.popen("adb shell pidof " + package)
    return contents.readlines()[0].strip()

def get_flow(pid): # 输入命令获取使用流量的值
    contents = os.popen("adb shell cat /proc/" + pid + "/net/dev")
    for line in contents.readlines():
        if 'wlan0:' in line:
            wlams = line.split(" ")
            return round(float((int(wlams[2]) + int(wlams[40]))/1024/1024/1024), 2)



















